import { call, takeLatest, put } from 'typed-redux-saga'
import { ForkEffect } from '@redux-saga/core/effects'
import { homeGetQuestions, homeUpdateState } from '@redux/action/homeActions'
import { getQuestions } from '@service/homeService'

function* getHomeQuestions() {
  const { data, message, state } = yield* call(getQuestions)
  if (state !== 'ok') return console.error(message)
  yield* put(homeUpdateState({ questionList: data }))
}

export default function* homeSaga(): Generator<ForkEffect<never>, void, unknown> {
  yield* takeLatest(homeGetQuestions, getHomeQuestions)
}
